/*
 * Table.java
 */
package mango.orm.entity.maker;

import java.util.ArrayList;
import java.util.List;

/**
 * 对于PDM文件中的一个表
 * 
 * @author zhangms
 */
public class EOTable {
	private static final String PREFIX = "EO";
	private static final String SUFFIX = "";
    private String javaClassName;
    private String id;
    private String name;
    private String code;
    private String comment;
    private String pkg;
    private List<EOColumn> columns = new ArrayList<EOColumn>();
    private List<EOKey> keys = new ArrayList<EOKey>();

    public String getJavaClassName() {
        return javaClassName;
    }

    public void setJavaClassName(String javaClassName) {
        this.javaClassName = javaClassName;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
        this.javaClassName = PREFIX + this.code + SUFFIX;
    }

    public String getComment() {
        return this.comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<EOColumn> getColumns() {
        return this.columns;
    }

    public void setColumns(List<EOColumn> columns) {
        this.columns = columns;
    }

    public List<EOKey> getKeys() {
        return this.keys;
    }

    public void setKeys(List<EOKey> keys) {
        this.keys = keys;
    }

    /**
     * @param primaryKey
     *            keyId
     */
    public void setPrimaryKey(String primaryKey) {
        if (null == primaryKey || primaryKey.length() == 0) {
            return;
        }
        for (EOKey key : this.keys) {
            if (key.getId().equals(primaryKey)) {
                List<String> keyColumns = key.getKeyColumns();
                for (String colId : keyColumns) {
                    for (EOColumn col : this.columns) {
                        if (col.getId().equals(colId)) {
                            col.setPrimary(true);
                            break;
                        }
                    }
                }
                break;
            }
        }
    }

    public void addColumn(EOColumn column) {
        this.columns.add(column);
    }

    public void addKey(EOKey key) {
        this.keys.add(key);
    }

    public String getPkg() {
        return this.pkg;
    }

    public void setPkg(String pkg) {
        this.pkg = pkg;
    }
}
